# -*- encoding: utf-8 -*-
'''
file       :public.py
Description: 公共函数存放位置
Date       :2022/12/22 09:36:35
Author     :Josco
version    :python3.7.8
'''
from config import *
import os


def load_ori_and_shoot_pic(ori_pic_file_path, pic_file_path):
    """
        desc: 根据原始图像存储路径和拍摄后的照片路径获取对应的照片
        param:
            ori_pic_file_path: 原始图像路径
            pic_file_path: 拍摄后的照片存储路径
    """
    ori_file_list = []  # 存储原始图像的路径
    shoot_pic_list = []  # 存储拍摄后图像的路径
    # 获取原始图像路径下的所有图像
    for top, dirs, nondirs in os.walk(ori_pic_file_path):
        for item in nondirs:
            if item.split('.')[-1] == "bmp":
                ori_file_list.append(os.path.join(top, item))
    # 获取拍摄后图像路径下的所有图像
    for top, dirs, nondirs in os.walk(pic_file_path):
        for item in nondirs:
            if item.split('.')[-1] == "bmp":
                shoot_pic_list.append(os.path.join(top, item))
    return ori_file_list, shoot_pic_list


def color_to_num(mat, r_thr, g_thr, b_thr):
    """使用此函数把矩阵中的0-255的值映射到0或255"""
    mat_b = mat[:, :, 0]  # 得到蓝色通道
    mat_g = mat[:, :, 1]  # 得到绿色通道
    mat_r = mat[:, :, 2]  # 得到红色通道

    mat_b = np.where((mat_b >= 0) & (mat_b < b_thr), 0, mat_b)
    mat_b = np.where((mat_b >= b_thr) & (mat_b <= 255), 255, mat_b)

    mat_g = np.where((mat_g >= 0) & (mat_g < g_thr), 0, mat_g)
    mat_g = np.where((mat_g >= g_thr) & (mat_g <= 255), 255, mat_g)

    mat_r = np.where((mat_r >= 0) & (mat_r < r_thr), 0, mat_r)
    mat_r = np.where((mat_r >= r_thr) & (mat_r <= 255), 255, mat_r)
    mat = np.dstack((mat_b, mat_g, mat_r))

    return mat


def color_to_zero_one(mat, r_thr, g_thr, b_thr):
    """使用此函数把矩阵中的0-255的色值映射到0, 1空间"""
    mat_b = mat[:, :, 0]  # 得到蓝色通道
    mat_g = mat[:, :, 1]  # 得到绿色通道
    mat_r = mat[:, :, 2]  # 得到红色通道

    mat_b = np.where((mat_b >= 0) & (mat_b < b_thr), 0, mat_b)
    mat_b = np.where((mat_b >= b_thr) & (mat_b <= 255), 1, mat_b)

    mat_g = np.where((mat_g >= 0) & (mat_g < g_thr), 0, mat_g)
    mat_g = np.where((mat_g >= g_thr) & (mat_g <= 255), 1, mat_g)

    mat_r = np.where((mat_r >= 0) & (mat_r < r_thr), 0, mat_r)
    mat_r = np.where((mat_r >= r_thr) & (mat_r <= 255), 1, mat_r)
    mat = np.dstack((mat_b, mat_g, mat_r))

    return mat


def get_matrix_all_row_col(dynamics_point_matrix, pic_local):
    """根据dynamics_point_matrix获取包括上中下左右五个点的row和col list, 选取规则按第几张图像来分辨"""
    _row = dynamics_point_matrix[:, 0]
    _col = dynamics_point_matrix[:, 1]
    # # 这是不带黑标的<0.05的配置
    # if pic_local == 0:
    #     return [[_row, _col], [_row, _col+1], [_row+1, _col]]
    # elif pic_local == 1:
    #     return [[_row, _col], [_row, _col+1],  [_row-1, _col]]
    # return [[_row, _col], [_row, _col+1], [_row+1, _col], [_row-1, _col], [_row+1, _col+1]]


    # if pic_local == 0:
    #     return [[_row,_col],[_row,_col+1],[_row-1,_col]]
    # elif pic_local == 1:
    #     return [[_row,_col],[_row,_col+1],[_row-1,_col],[_row-1,_col+1]]
    # return [[_row,_col],[_row,_col+1],[_row-1,_col]]

    
    return [[_row, _col],[_row,_col+1],[_row-1,_col]]
